package com.ruoyi.user.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.user.domain.PersonInfo;
import com.ruoyi.user.mapper.OrdersMapper;
import com.ruoyi.user.domain.Orders;
import com.ruoyi.user.service.IOrdersService;

/**
 * 用药记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-21
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 查询用药记录
     * 
     * @param orderId 用药记录主键
     * @return 用药记录
     */
    @Override
    public Orders selectOrdersByOrderId(Long orderId)
    {
        return ordersMapper.selectOrdersByOrderId(orderId);
    }

    /**
     * 查询用药记录列表
     * 
     * @param orders 用药记录
     * @return 用药记录
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增用药记录
     * 
     * @param orders 用药记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOrders(Orders orders)
    {
        orders.setCreateTime(DateUtils.getNowDate());
        int rows = ordersMapper.insertOrders(orders);
        insertPersonInfo(orders);
        return rows;
    }

    /**
     * 修改用药记录
     * 
     * @param orders 用药记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOrders(Orders orders)
    {
        ordersMapper.deletePersonInfoByPersonId(orders.getOrderId());
        insertPersonInfo(orders);
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除用药记录
     * 
     * @param orderIds 需要删除的用药记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrdersByOrderIds(Long[] orderIds)
    {
        ordersMapper.deletePersonInfoByPersonIds(orderIds);
        return ordersMapper.deleteOrdersByOrderIds(orderIds);
    }

    /**
     * 删除用药记录信息
     * 
     * @param orderId 用药记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrdersByOrderId(Long orderId)
    {
        ordersMapper.deletePersonInfoByPersonId(orderId);
        return ordersMapper.deleteOrdersByOrderId(orderId);
    }

    /**
     * 新增老人信息信息
     * 
     * @param orders 用药记录对象
     */
    public void insertPersonInfo(Orders orders)
    {
        List<PersonInfo> personInfoList = orders.getPersonInfoList();
        Long orderId = orders.getOrderId();
        if (StringUtils.isNotNull(personInfoList))
        {
            List<PersonInfo> list = new ArrayList<PersonInfo>();
            for (PersonInfo personInfo : personInfoList)
            {
                personInfo.setPersonId(orderId);
                list.add(personInfo);
            }
            if (list.size() > 0)
            {
                ordersMapper.batchPersonInfo(list);
            }
        }
    }
}
